package org.example.spring.carsalemanagementsystem.service.impl;

import lombok.RequiredArgsConstructor;
import org.example.spring.carsalemanagementsystem.dao.CommodityDao;
import org.example.spring.carsalemanagementsystem.entity.Commodity;
import org.example.spring.carsalemanagementsystem.service.CommodityService;
import org.example.spring.carsalemanagementsystem.utils.IDUtil;
import org.example.spring.carsalemanagementsystem.utils.Page;
import org.example.spring.carsalemanagementsystem.vo.CommodityFuzzyVo;
import org.example.spring.carsalemanagementsystem.vo.CommodityVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CommodityServiceImpl implements CommodityService {
    private final CommodityDao commodityDao;
    private final AccessoryServiceImpl accessoryService;
    @Override
    @Transactional
    public void deleteById(String carid) {
        commodityDao.deleteById(carid);
        //删除对应所有附属品
        accessoryService.deleteByCarId(carid);
    }
    @Override
    public void insertOne(Commodity commodity) {
        commodity.setCarid(IDUtil.getId());
        commodity.setCreatetime(LocalDateTime.now());
        commodityDao.insertOne(commodity);
    }
    @Override
    public void updateById(Commodity commodity) {
        commodityDao.updateById(commodity);
    }
    @Override
    public Page<Commodity> findPages(Long pageNum, Long pageSize) {
        Page<Commodity> page=new Page<>();
        List<Commodity> commodities = commodityDao.findPages(pageNum, pageSize);
        Long total = commodityDao.count();
        page.setTotal(total);
        page.setRows(commodities);
        return page;
    }
    @Override
    public Page<Commodity> findPagesByKey(CommodityFuzzyVo vo) {
        Page<Commodity> page=new Page<>();
        List<Commodity> commodities = commodityDao.findPagesByKey(vo);
        Long total = commodityDao.countByKey(vo);
        page.setTotal(total);
        page.setRows(commodities);
        return page;
    }
    @Override
    public List<CommodityVo> findList() {
        return commodityDao.findList();
    }
    @Override
    public List<CommodityVo> findListByKey(String key) {
        return commodityDao.findListByKey(key);
    }

}
